﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using System.Collections.Generic;
using System.IO;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class ModifyMMS : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            { 
                int mmsId = int.Parse(Request.QueryString["MmsId"]);
                int projectId = int.Parse(Request.QueryString["ProjectId"]);
                NxMms nxMms = ServiceLocator.IProjectService.GetNxMmsById(mmsId);
                IList<NxMms> nxMMS = ServiceLocator.IProjectService.GetNxMmsCollection(projectId);
                NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
                this.txtMmsTitle.Text = nxProject.MmsTitle;
                this.txtMmsText.Text = nxMms.MmsText;
                this.ImgMmsPic.ImageUrl = nxMms.MmsPic;
                this.rblMMSPosition.Items[nxMms.MmsPosition-1].Selected = true;
                this.txtDelayTime.Text = nxMms.DelayTime.ToString();
                int mmsPage = nxMms.MmsPage;
                int mmsPageCount = nxMMS.Count;
                for (int i = 0; i < mmsPageCount; i++)
                {
                    this.ddlMmsPage.Items.Add(new ListItem((String)GetGlobalResourceObject("Default", "THE") + (i + 1).ToString() + (String)GetGlobalResourceObject("Default", "PAGE"), (i + 1).ToString()));
                }
                this.ddlMmsPage.SelectedIndex = mmsPage - 1;
                if (mmsPage == 1)
                    this.btnBack.Visible = false;
                else
                    this.btnBack.Visible = true;
            }
        }

        protected void ddlMmsPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            int mmsPage = int.Parse(this.ddlMmsPage.SelectedValue);
            NxMms nxMms = ServiceLocator.IProjectService.GetNxMmsByPage(projectId,mmsPage);
            Response.Redirect("MMSView.aspx?MmsId=" + nxMms.MmsId + "&ProjectId=" + projectId);
        }

        protected void btnModifyImg_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            int mmsId = int.Parse(Request.QueryString["MmsId"]);
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            NxMms nxMms = ServiceLocator.IProjectService.GetNxMmsById(mmsId);
            NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
            if(this.Panel1.Visible == true && this.Panel2.Visible == false)
            {
                nxProject.MmsTitle = this.txtMmsTitle.Text.Trim();
                nxMms.MmsText = this.txtMmsText.Text.Trim();
                nxMms.MmsPosition = int.Parse(this.rblMMSPosition.SelectedValue);
                nxMms.DelayTime = int.Parse(this.txtDelayTime.Text.Trim());
                if(ServiceLocator.IProjectService.UpdateNxMms(nxMms) && ServiceLocator.IProjectService.UpdateNxProject(nxProject))
                {
                    Response.Redirect("MMSView.aspx?MmsId=" + mmsId + "&ProjectId=" + projectId);
                }
            }
            if (this.Panel1.Visible == false && this.Panel2.Visible == true)
            {
                //上传彩信图片
                try
                {
                    if (this.fuMMSImage.HasFile)
                    {
                        //判断图片格式是否正确
                        string photoExt = Path.GetExtension(this.fuMMSImage.FileName).ToLower();
                        if (photoExt == ".gif" || photoExt == ".jpg")
                        {
                            string name = this.fuMMSImage.PostedFile.FileName; //文件路径
                            FileInfo fileInfo = new FileInfo(name);

                            Random rd = new Random();
                            string fileName = DateTime.Now.ToString("yyyyMMddmmhhss") + rd.Next(10, 99) + photoExt;   //文件名称
                            string sFileName = "x_" + fileName;             //缩略图文件名 

                            string filePath = Server.MapPath("~/ImgUpLoad/" + fileName);    //服务器端文件路径
                            string sFilePath = Server.MapPath("~/ImgUpLoad/" + sFileName);  //缩略图文件路径

                            int w = 240;
                            int h = 0;

                            this.fuMMSImage.SaveAs(filePath);
                            ThumbNail.MakeThumNail(filePath, sFilePath, w, h, "W");

                            //更新彩信
                            nxProject.MmsTitle = this.txtMmsTitle.Text;
                            nxMms.MmsPage = int.Parse(ddlMmsPage.SelectedValue.ToString());
                            nxMms.MmsPic = @"ImgUpLoad/" + sFileName;//只保存缩略图地址
                            nxMms.ProjectId = projectId;
                            nxMms.MmsPosition = int.Parse(rblMMSPosition.SelectedValue);
                            nxMms.MmsText = this.txtMmsText.Text.Trim();
                            nxMms.DelayTime = int.Parse(this.txtDelayTime.Text.Trim());

                            if (ServiceLocator.IProjectService.UpdateNxMms(nxMms) && ServiceLocator.IProjectService.UpdateNxProject(nxProject))
                            {
                                Response.Redirect("MMSView.aspx?MmsId=" + mmsId + "&ProjectId=" + projectId);
                            }

                        }
                        else
                        {
                            JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "PICTURE_FORMAT") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                            return;
                        }
                    }
                    else
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "PLEASE_UPLOAD_A_PHOTO") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    JavaScript.WriteJs("jAlert('" + ex.ToString() + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                    return;
                }
            }
            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            int mmsPage = int.Parse(this.ddlMmsPage.SelectedValue) - 1;
            NxMms nxMms = ServiceLocator.IProjectService.GetNxMmsByPage(projectId, mmsPage);
            Response.Redirect("ModifyMms.aspx?MmsId=" + nxMms.MmsId + "&ProjectId=" + projectId);            
        }
    }
}
